package com.itbaiyun.agent.app;

import com.itbaiyun.agent.entities.LoveReport;
import jakarta.annotation.Resource;
import jakarta.annotation.Resources;
import lombok.extern.slf4j.Slf4j;
import org.springframework.ai.chat.client.ChatClient;
import org.springframework.ai.chat.client.advisor.MessageChatMemoryAdvisor;
import org.springframework.ai.chat.client.advisor.QuestionAnswerAdvisor;
import org.springframework.ai.chat.memory.ChatMemory;
import org.springframework.ai.chat.memory.InMemoryChatMemory;
import org.springframework.ai.chat.model.ChatModel;
import org.springframework.ai.chat.model.ChatResponse;
import org.springframework.ai.tool.ToolCallback;
import org.springframework.ai.tool.ToolCallbacks;
import org.springframework.ai.vectorstore.SimpleVectorStore;
import org.springframework.ai.vectorstore.VectorStore;
import org.springframework.stereotype.Component;

import static org.springframework.ai.chat.client.advisor.AbstractChatMemoryAdvisor.CHAT_MEMORY_CONVERSATION_ID_KEY;
import static org.springframework.ai.chat.client.advisor.AbstractChatMemoryAdvisor.CHAT_MEMORY_RETRIEVE_SIZE_KEY;

@Component
@Slf4j
public class MyAgent {

    @Resource
    private VectorStore myVectorStore;
    @Resource
    private ToolCallback[]  allTools;

    private final ChatClient chatClient;

    private static final String SYSTEM_PROMPT = "扮演深耕恋爱心理领域的专家。开场向用户表明身份，告知用户可倾诉恋爱难题。" +
            "围绕单身、恋爱、已婚三种状态提问：单身状态询问社交圈拓展及追求心仪对象的困扰；" +
            "恋爱状态询问沟通、习惯差异引发的矛盾；已婚状态询问家庭责任与亲属关系处理的问题。" +
            "引导用户详述事情经过、对方反应及自身想法，以便给出专属解决方案。";


    private static final String SYSTEM_PROMPT1 = "北京协和医院是一所历史悠久的医院。它在医疗领域有着卓越的成就。\n" +
            "医院拥有众多知名专家。他们为患者提供了高质量的医疗服务。\n" +
            "医院的设施先进。这为诊断和治疗提供了有力支持。\n" +
            "此外，医院还注重科研和教学。培养了许多优秀的医学人才。";


    /**
     * 初始化聊天机器人
     *
     * @param dashscopeChatModel
     */
    public MyAgent(ChatModel dashscopeChatModel) {
//        ChatMemory chatMemory = MessageWindowChatMemory.builder()
//                .chatMemoryRepository(chatMemoryRepository)
//                .maxMessages(10)
//                .build();
        ChatMemory chatMemory = new InMemoryChatMemory();
        chatClient = ChatClient.builder(dashscopeChatModel)
                .defaultSystem(SYSTEM_PROMPT)
                .defaultAdvisors(
                        new MessageChatMemoryAdvisor(chatMemory)
                )
                .build();
    }

    //支持多轮对话(聊天记忆)
    public String doChatMemory(String text, String chatId) {
        return chatClient.prompt(SYSTEM_PROMPT)
                .user(text)
                .advisors(a -> a.param(CHAT_MEMORY_CONVERSATION_ID_KEY, chatId)
                        .param(CHAT_MEMORY_RETRIEVE_SIZE_KEY, 10))
                .call()
                .content();
    }

    //支持结构化输出
    public LoveReport doChatStruct(String text, String chatId) {
        return chatClient.prompt(SYSTEM_PROMPT1)
                .user(text)
                .advisors(a -> a.param(CHAT_MEMORY_CONVERSATION_ID_KEY, chatId)
                        .param(CHAT_MEMORY_RETRIEVE_SIZE_KEY, 10))
                .call()
                .entity(LoveReport.class);
    }


    //支持RAG增强检索
    public String doChatRag(String text, String chatId) {

        ChatResponse chatResponse = chatClient.prompt(SYSTEM_PROMPT1)
                .user(text)
                .advisors(new QuestionAnswerAdvisor(myVectorStore))
                .advisors(a -> a.param(CHAT_MEMORY_CONVERSATION_ID_KEY, chatId)
                        .param(CHAT_MEMORY_RETRIEVE_SIZE_KEY, 10))
//                .call()
//                .content();
                .call()
                .chatResponse();
        return chatResponse.getResult().getOutput().getText();
    }


    //支持工具调用
    public String doChatWithTools(String text, String chatId) {
        ChatResponse chatResponse = chatClient.prompt(SYSTEM_PROMPT1)
                .user(text)
                .advisors(new QuestionAnswerAdvisor(myVectorStore))
                .advisors(a -> a.param(CHAT_MEMORY_CONVERSATION_ID_KEY, chatId)
                        .param(CHAT_MEMORY_RETRIEVE_SIZE_KEY, 10))
                .tools(allTools)
                .call()
                .chatResponse();
        return chatResponse.getResult().getOutput().getText();
    }

}
